﻿using Microsoft.AspNetCore.Mvc;
using XJay.Scheduler.Model.Job;
using XJay.Scheduler.JobEntity;
using Quartz;
using XJay.Scheduler.Common.CustomException;
using Microsoft.AspNetCore.Authorization;

namespace XJay.Scheduler.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Policy = "AdminPolicy")]
    public class JobController : ControllerBase
    {
        private readonly ISchedulerrManger _schedulerManger;

        public JobController(ISchedulerrManger schedulerManger)
        {
            this._schedulerManger = schedulerManger;
        }

        /// <summary>
        /// 添加job
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> AddJob(AddJobParams param)
        {
            await _schedulerManger.AddCronJobAsync(new CronEntity() { 
                Cron = param.Cron,
                DllFileName = param.DllPath,
                JobClass = param.JobClass,
                JobGroup = param.JobGroup,
                JobName = param.JobName,
                Description = param.Description
            });       

            return true;
        }

        /// <summary>
        /// 为xxJob添加一个触发器
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> AddTriggerJobAsync(TriggerEntity param)
        {
            await _schedulerManger.AddTriggerJobAsync(param);

            return true;
        }

        /// <summary>
        /// 删除job
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteJob(DelJobParams param)
        {
            return await _schedulerManger.DeleteJobAsync(new JobKey(param.JobName, param.JobGroup));
        }
    }
}
